package com.lidx.back.statistics.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lidx.back.statistics.commons.MaterialAction;
import com.lidx.back.statistics.entity.MaterialCount;
import com.lidx.back.statistics.entity.MaterialDownload;
import com.lidx.back.statistics.entity.MaterialMakeVideo;
import com.lidx.back.statistics.entity.MaterialRecord;
import com.lidx.back.statistics.service.IMaterialCountService;
import com.lidx.back.statistics.service.IMaterialRecordService;

/**
 * 素材相关的统计的接口
 * @author cwd
 *
 */
@RestController
@RequestMapping(value="material")
public class MaterialCountController {
	
	@Autowired
	private IMaterialCountService countService;
	@Autowired
	private IMaterialRecordService recordService;
	
	@RequestMapping(value="download",method=RequestMethod.POST)
	public void downloadMaterial(@RequestBody CountRequest request){
		MaterialRecord materialDownload = new MaterialRecord();
		VersionBean version = request.getVersion();
		String uuid = request.getMaterialUuid();
		String deviceId = request.getDeviceId();
		if(null != request&&null != version && !StringUtils.isEmpty(uuid) && !StringUtils.isEmpty(deviceId)){
			if(!StringUtils.isEmpty(version.getVersionNumber())
					&&!StringUtils.isEmpty(version.getVersionType())){
				materialDownload.setUuid(uuid);
				materialDownload.setVersionNum(version.getVersionNumber());
				materialDownload.setVersionType(version.getVersionType());
				materialDownload.setDeviceId(deviceId); 
				materialDownload.setAction(MaterialAction.MATERIAL_DOWNLOAD);
				recordService.saveRecord(materialDownload);
			}
			
		}
	}
	@RequestMapping(value="makeVideo",method=RequestMethod.POST)
	public void makeVideo(@RequestBody CountRequest request){
		MaterialRecord count = new MaterialRecord();
		VersionBean version = request.getVersion();
		String uuid = request.getMaterialUuid();
		if(null != request&&null != version && !StringUtils.isEmpty(uuid)){
			if(!StringUtils.isEmpty(version.getVersionNumber())
					&&!StringUtils.isEmpty(version.getVersionType())){
				count.setUuid(uuid);
				count.setVersionNum(version.getVersionNumber());
				count.setVersionType(version.getVersionType());
				count.setDeviceId(request.getDeviceId());
				count.setAction(MaterialAction.MATERIAL_MAKE_VIDEO);
				recordService.saveRecord(count);
			}
			
		}
	}
	
	@RequestMapping(value="photograph",method=RequestMethod.POST)
	public void photographWithMaterial(@RequestBody CountRequest request){
		MaterialRecord count = new MaterialRecord();
		VersionBean version = request.getVersion();
		String uuid = request.getMaterialUuid();
		if(null != request&&null != version && !StringUtils.isEmpty(uuid)){
			if(!StringUtils.isEmpty(version.getVersionNumber())
					&&!StringUtils.isEmpty(version.getVersionType())){
				count.setUuid(uuid);
				count.setVersionNum(version.getVersionNumber());
				count.setVersionType(version.getVersionType());
				count.setUuid(uuid);
				count.setDeviceId(request.getDeviceId());
				count.setAction(MaterialAction.MATERIAL_PHOTOGRAPH);
				recordService.saveRecord(count);
			}
			
		}
	}
	
	@RequestMapping(value="picShare",method=RequestMethod.POST)
	public void picShareWithMaterial(@RequestBody CountRequestWithChannel request){
		MaterialRecord count = new MaterialRecord();
		VersionBean version = request.getVersion();
		String uuid = request.getMaterialUuid();
		String channel = request.getChannel();
		if(null != request&&null != version && !StringUtils.isEmpty(uuid)){
			if(!StringUtils.isEmpty(version.getVersionNumber())
					&& !StringUtils.isEmpty(version.getVersionType())
					&& !StringUtils.isEmpty(channel)){
				count.setUuid(uuid);
				count.setVersionNum(version.getVersionNumber());
				count.setVersionType(version.getVersionType());
				count.setUuid(uuid);
				count.setDeviceId(request.getDeviceId());
				count.setAction(MaterialAction.MATERIAL_PIC_SHARE);
				count.setChannel(channel);
				recordService.saveRecord(count);
			}
			
		}
	}
	
	@RequestMapping(value="videoShare",method=RequestMethod.POST)
	public void videoShareWithMaterial(@RequestBody CountRequestWithChannel request){
		MaterialRecord count = new MaterialRecord();
		VersionBean version = request.getVersion();
		String uuid = request.getMaterialUuid();
		String channel = request.getChannel();
		if(null != request&&null != version && !StringUtils.isEmpty(uuid)){
			if(!StringUtils.isEmpty(version.getVersionNumber())
					&&!StringUtils.isEmpty(version.getVersionType())
					&& !StringUtils.isEmpty(channel)){
				count.setUuid(uuid);
				count.setVersionNum(version.getVersionNumber());
				count.setVersionType(version.getVersionType());
				count.setUuid(uuid);
				count.setDeviceId(request.getDeviceId());
				count.setAction(MaterialAction.MATERIAL_VIDEO_SHARE);
				count.setChannel(channel);
				recordService.saveRecord(count);
			}
			
		}
	}
}
